package com.feather.common.data.simple;

import java.util.ArrayList;
import java.util.List;

import com.feather.common.data.DataColumn;
import com.feather.common.data.DataColumnCollection;

/**
 * @author flogyin
 */
public class SimpleDataColumnCollection implements DataColumnCollection, java.io.Serializable {
	private static final long		serialVersionUID	= 1L;

	protected ArrayList<DataColumn>	collection			= new ArrayList<DataColumn>();

	@Override
	public int getCount() {
		return this.collection.size();
	}

	@Override
	public DataColumn get(int columnIndex) {
		return this.collection.get(columnIndex);
	}

	@Override
	public DataColumn get(String columnName) {
		int columnIndex = indexOf(columnName);
		return columnIndex > -1 ? this.collection.get(columnIndex) : null;
	}

	// public DataColumn add() {
	// return add((String)null);
	// }
	public void add(DataColumn column) {
		if (column != null)
			this.collection.add(column);
	}

	// public DataColumn add(String columnName)
	// {
	// SimpleColumn column = new SimpleColumn();
	// column.setName(columnName);
	// SimpleDataColumn dataColumn = new SimpleDataColumn(column);
	// this.collection.add(dataColumn);
	// return dataColumn;
	// }

	@Override
	public boolean contains(String columnName) {
		return indexOf(columnName) > -1;
	}

	@Override
	public int indexOf(DataColumn column) {
		return column != null ? indexOf(column.getColumn().getName()) : -1;
	}

	@Override
	public int indexOf(String columnName) {
		if (columnName != null) {
			int count = getCount();
			for (int i = 0; i < count; i++) {
				String temp = this.collection.get(i).getColumn().getName();
				if (columnName.equalsIgnoreCase(temp))
					return i;
			}
		}
		return -1;
	}

	@Override
	public List<Object> getList() {
		List<Object> list = new ArrayList<Object>();
		int count = getCount();
		for (int i = 0; i < count; i++) {
			DataColumn dataColumn = get(i);
			list.add(dataColumn.getObject());
		}
		return list;
	}

	@Override
	public Object[] getArray() {
		return getList().toArray();
	}

	@Override
	public void remove(DataColumn column) {
		int columnIndex = indexOf(column);
		if (columnIndex > -1)
			this.collection.remove(columnIndex);
	}

	@Override
	public void remove(String columnName) {
		int columnIndex = indexOf(columnName);
		if (columnIndex > -1)
			this.collection.remove(columnIndex);
	}

	@Override
	public void clear() {
		this.collection.clear();
	}
}
